package com.endes.android.model.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.endes.android.entidades.SSEC03;
import com.endes.android.entidades.SSEC04;
import com.endes.android.model.database.EndesOpenHelper;

public class QSSec4Dao {
	private EndesOpenHelper endesOpenHelper;
	private SQLiteDatabase db;
	private Cursor cursor = null;
	private Context context;

	public QSSec4Dao(Context context) {
		this.context = context;
		endesOpenHelper = new EndesOpenHelper(context);

	}

	public SSEC04 ObtenerSeccion4(int qsCluster, int qsNumber, int qsHome,
			int numeroOrdenPersonaSeleccionada) {
		SSEC04 ssec04 = null;
		db = endesOpenHelper.getReadableDatabase();
		cursor = db
				.rawQuery(
						"Select * from SSEC04 where qsCluster=? and qsNumber=? and qsHome=? and qs20C=?",
						new String[] { qsCluster + "", qsNumber + "",
								qsHome + "",
								numeroOrdenPersonaSeleccionada + "" });
		if (cursor.moveToFirst()) {
			ssec04 = new SSEC04();
			ssec04.setQsCluster(qsCluster);
			ssec04.setQsNumber(qsNumber);
			ssec04.setQsHome(qsHome);
			ssec04.setQs20C(numeroOrdenPersonaSeleccionada);
			ssec04.setQs401(cursor.getInt(cursor.getColumnIndex("qs401")));
			ssec04.setQs402(cursor.getInt(cursor.getColumnIndex("qs402")));
			ssec04.setQs403(cursor.getInt(cursor.getColumnIndex("qs403")));
			ssec04.setQs404(cursor.getInt(cursor.getColumnIndex("qs404")));
			ssec04.setQs406(cursor.getInt(cursor.getColumnIndex("qs406")));
			ssec04.setQs407(cursor.getString(cursor.getColumnIndex("qs407")));
			ssec04.setQs407XC(cursor.getString(cursor.getColumnIndex("qs407XC")));
			ssec04.setQs407X(cursor.getString(cursor.getColumnIndex("qs407X")));
			ssec04.setQs409(cursor.getInt(cursor.getColumnIndex("qs409")));
			ssec04.setQs410U(cursor.getInt(cursor.getColumnIndex("qs410U")));
			ssec04.setQs410C(cursor.getInt(cursor.getColumnIndex("qs410C")));
			ssec04.setQs411(cursor.getInt(cursor.getColumnIndex("qs411")));
			ssec04.setQs412U(cursor.getInt(cursor.getColumnIndex("qs412U")));
			ssec04.setQs412C(cursor.getInt(cursor.getColumnIndex("qs412C")));
			ssec04.setQs413(cursor.getInt(cursor.getColumnIndex("qs413")));
			ssec04.setQs415(cursor.getInt(cursor.getColumnIndex("qs415")));
			ssec04.setQs416U(cursor.getInt(cursor.getColumnIndex("qs416U")));
			ssec04.setQs416C(cursor.getInt(cursor.getColumnIndex("qs416C")));
		}
		Cerrar();
		return ssec04;
	}

	public void guardarSec4Encuesta(SSEC04 ssec04) {
		ContentValues contenido = new ContentValues();
		contenido.put("qs401", ssec04.getQs401());
		contenido.put("qs402", ssec04.getQs402());
		contenido.put("qs403", ssec04.getQs403());
		contenido.put("qs404", ssec04.getQs404());
		if(verificarEdadPregunta23(ssec04.getQsCluster(), ssec04.getQsNumber(),
			ssec04.getQsHome(), ssec04.getQs20C()) >= 40 &&
			verificarEdadPregunta23(ssec04.getQsCluster(), ssec04.getQsNumber(),
						ssec04.getQsHome(), ssec04.getQs20C()) <= 59){
			contenido.put("qs406", ssec04.getQs406());
			contenido.put("qs407", ssec04.getQs407());
			contenido.put("qs407XC", ssec04.getQs407XC());
			contenido.put("qs407X", ssec04.getQs407X());
			if(verificarSexoPersona(ssec04.getQsCluster(), ssec04.getQsNumber(),
					ssec04.getQsHome(), ssec04.getQs20C()) == 2){
				if(verificarEdadPregunta23(ssec04.getQsCluster(), ssec04.getQsNumber(),
						ssec04.getQsHome(), ssec04.getQs20C()) >= 30 &&
						verificarEdadPregunta23(ssec04.getQsCluster(), ssec04.getQsNumber(),
						ssec04.getQsHome(), ssec04.getQs20C()) <= 59){
					contenido.put("qs409", ssec04.getQs409());
					contenido.put("qs410U", ssec04.getQs410U());
					contenido.put("qs410C", ssec04.getQs410C());
					contenido.put("qs411", ssec04.getQs411());
					contenido.put("qs412U", ssec04.getQs412U());
					contenido.put("qs412C", ssec04.getQs412C());
					contenido.put("qs413", ssec04.getQs413());
					if(verificarEdadPregunta23(ssec04.getQsCluster(), ssec04.getQsNumber(),
							ssec04.getQsHome(), ssec04.getQs20C()) >= 40 &&
							verificarEdadPregunta23(ssec04.getQsCluster(), ssec04.getQsNumber(),
							ssec04.getQsHome(), ssec04.getQs20C()) <= 59){
						contenido.put("qs415", ssec04.getQs412U());
						contenido.put("qs416U", ssec04.getQs412C());
						contenido.put("qs416C", ssec04.getQs413());
					}
				}
			}
		} else{
			if(verificarSexoPersona(ssec04.getQsCluster(), ssec04.getQsNumber(),
					ssec04.getQsHome(), ssec04.getQs20C()) == 2){
				if(verificarEdadPregunta23(ssec04.getQsCluster(), ssec04.getQsNumber(),
						ssec04.getQsHome(), ssec04.getQs20C()) >= 30 &&
						verificarEdadPregunta23(ssec04.getQsCluster(), ssec04.getQsNumber(),
						ssec04.getQsHome(), ssec04.getQs20C()) <= 59){
					contenido.put("qs409", ssec04.getQs409());
					contenido.put("qs410U", ssec04.getQs410U());
					contenido.put("qs410C", ssec04.getQs410C());
					contenido.put("qs411", ssec04.getQs411());
					contenido.put("qs412U", ssec04.getQs412U());
					contenido.put("qs412C", ssec04.getQs412C());
					contenido.put("qs413", ssec04.getQs413());
				} 
			} 
		}
		if (ExisteSSEC04(ssec04)) {
			db = endesOpenHelper.getWritableDatabase();
			db.update(
					"SSEC04",
					contenido,
					"qsCluster=? and qsNumber=? and qsHome=? and qs20C=?",
					new String[] { ssec04.getQsCluster() + "",
							ssec04.getQsNumber() + "", ssec04.getQsHome() + "",
							ssec04.getQs20C() + "" });
			db.close();
		} else {
			db = endesOpenHelper.getWritableDatabase();
			contenido.put("qsCluster", ssec04.getQsCluster());
			contenido.put("qsNumber", ssec04.getQsNumber());
			contenido.put("qsHome", ssec04.getQsHome());
			contenido.put("qs20C", ssec04.getQs20C());
			db.insert("SSEC04", null, contenido);
			db.close();
		}
	}

	public boolean ExisteSSEC04(SSEC04 ssec04) {
		db = endesOpenHelper.getReadableDatabase();
		cursor = db.rawQuery(
				"Select * from SSEC04 where qsCluster=? and qsNumber=? and qsHome=? "
						+ "and qs20C=?", new String[] {
						ssec04.getQsCluster() + "", ssec04.getQsNumber() + "",
						ssec04.getQsHome() + "", ssec04.getQs20C() + "" });
		if (cursor.moveToFirst()) {
			Cerrar();
			return true;
		} else {
			Cerrar();
			return false;
		}
	}
	
	public int verificarEdadPregunta23(int qsCluster, int qsNumber, int qsHome,
			int numeroOrdenPersonaSeleccionada){
		int edad = 0;
		db = endesOpenHelper.getReadableDatabase();
		cursor = db
				.rawQuery(
						"Select qs23 from SSEC01 where qsCluster=? and qsNumber=? and qsHome=? and qs20C=?",
						new String[] { qsCluster + "", qsNumber + "",
								qsHome + "",
								numeroOrdenPersonaSeleccionada + "" });
		if (cursor.moveToFirst()) {
			edad = cursor.getInt(cursor.getColumnIndex("qs23"));
		}
		return edad;
	}
	
	public int verificarSexoPersona(int qsCluster, int qsNumber, int qsHome,
			int numeroOrdenPersonaSeleccionada){
		int sexo = 0;
		db = endesOpenHelper.getReadableDatabase();
		cursor = db
				.rawQuery(
						"Select qsSexo from SSEC01 where qsCluster=? and qsNumber=? and qsHome=? and qs20C=?",
						new String[] { qsCluster + "", qsNumber + "",
								qsHome + "",
								numeroOrdenPersonaSeleccionada + "" });
		if (cursor.moveToFirst()) {
			sexo = cursor.getInt(cursor.getColumnIndex("qsSexo"));
		}
		return sexo;
	}

	public void Cerrar() {
		cursor.close();
		db.close();
	}

}
